import { _angleRad, _angleToRad, _deg, _dis, _mid, _o, _polarRad, _rotate, _moveDis } from "../math/index.js"
import { checkSweepFlag, checkPolygonSweepFlag } from '../math/vec.js'
import { _alphabet } from '../common/labels.js'
import { getBoundingClientRect } from './shapeRect.js'
export const markInfo = (points, options) => {

    let { angle = true, edge = true, r = 20, radius = false, labels = false, boundingClientRect = false } = options
    let len = points.length
    let es = []
    let es1 = []
    let es2 = []
    let as = []
    let polygonSweepFlag = checkPolygonSweepFlag(points)
    let rs = []
    let o = _o(points)
    let ls = []
    let bs = []

    points.forEach((t, i) => {
        // 逆时针 ===1 
        let sweepFlag = checkSweepFlag(points, i)
        let prev = points[i - 1 < 0 ? len - 1 : i - 1]
        let next = points[(i + 1) % len]

        let a = _angleRad(t, next)
        let ap = _angleRad(t, prev)
        let ia = _angleToRad(prev, t, next)

        // 夹角
        if (angle) {
            let pn = _polarRad(t, r, a)
            let PP = _polarRad(t, r, ap)

            as.push({
                points: [PP, t, pn],
                a,
                ia,
                sweepFlag,
                o: t,
                r,
                labelPos: _polarRad(t, r * 2, sweepFlag === 0 ? (a - ia / 2) : (a + ia / 2)),
                label: _deg(ia) + '°'
            })
        }
        // 半径
        if (radius) {
            rs.push({
                points: [o, t],
                labelPos: _mid(o, t),
                label: _dis(o, t)

            })
        }

        if (labels) {
            ls.push({
                labelPos: _moveDis(t, o, r),
                label: _alphabet(i)
            })
        }



        // 逆时针 
        let k = polygonSweepFlag === 1 ? -1 : 1

        let pn = _polarRad(t, r * 2, a - k * Math.PI / 2)
        es1.push([t, pn])
        let PP = _polarRad(t, r * 2, ap + k * Math.PI / 2)
        es2.push([t, PP])

    })

    // 边长度
    if (edge) {
        let _es = (es1, es2) => {
            es1.forEach((t, i) => {
                let next = es2[(i + 1) % len]
                let m = _mid(t[1], next[1])
                let d = _dis(t[1], next[1])
                es.push({
                    points: [...t, next[1], next[0]],
                    label: d,
                    labelPos: m
                })
            })
        }
        _es(es1, es2)
    }

    if (boundingClientRect) {
        let rect = getBoundingClientRect(points)
        let { left, right, top, bottom } = rect


        let by = bottom[1] + r * 3
        bs.push({
            points: [left, [left, by], [right, by], right],
            label: right - left,
            labelPos: [(right + left) / 2, by]
        })


        let lx = left[0] - r * 3
        bs.push({
            points: [bottom, [lx, bottom], [lx, top], top],
            label: bottom - top,
            labelPos: [lx, (bottom + top) / 2]
        })

    }



    return {
        as,
        es,
        rs,
        ls,
        bs
    }
}